using System;
using System.Collections.Generic;
using System.Text;

namespace TuringMachine
{
    public class Tape
    {
        #region Private members
        /// <summary>
        /// Input on tape
        /// </summary>
        private string tapeInput;// = new char[UInt16.MaxValue];

        
        /// <summary>
        /// Index of cursor place on tape
        /// </summary>
        private UInt16 position;

        

        
        #endregion

        #region Getters / Setters
        public UInt16 Position
        {
            get { return position; }
            set { position = value; }
        }
        public String TapeInput
        {
            get 
            {
                /*string _ret = new string(tapeInput);
                return _ret;*/
                return tapeInput;
            }
            set {
                /*for (int i = 0; i < value.Length-1; i++)
                {
                    tapeInput[i] = value[i];
                }*/
                tapeInput = value;
                if (!tapeInput.EndsWith(" "))
                {
                    tapeInput = tapeInput + " ";
                }
            }
        }
        public Char CurrentChar
        {
            get { return tapeInput[position]; }
            set 
            {
                if (position < tapeInput.Length)
                {
                    Char[] newInput;
                    newInput = tapeInput.ToCharArray();
                    newInput[position] = value;
                    tapeInput =new string(newInput);
                    
                }
                //tapeInput[position] = value;
                
            }
        }
        #endregion

        #region Constructors
        /*public Tape()
        {
            this.TapeInput = String.Empty;
            this.Position = 0;
        }
        public Tape(String input)
        {
            this.TapeInput = input;
            this.Position = 0;
        }*/
        public Tape(String input, UInt16 position)
        {
            this.TapeInput = input;
            this.Position = position;
        }
        #endregion

        #region Public methods
        public void MoveLeft()
        {
            if (position > UInt16.MinValue)
            {
                position--;
            }
            else
            {
                throw new Exception("Position out of tape");
            }
        }
        public void MoveRight()
        {
            if (position < UInt16.MaxValue)
            {
                position++;
            }
            else
            {
                throw new Exception("Position out of tape");
            }
        }

        #endregion

        #region Private Methods
        /*private void CleanInput()
        {
            for (int i = 0; i < UInt16.MaxValue; i++)
            {
                tapeInput[i] = ' ';
            }
        }*/
        #endregion
    }
}
